﻿using System.Collections;

namespace FireFly
{
    /// <summary>
    /// Remove item command in common scenaries
    /// </summary>
    public class CommonRemoveItemCommand<TViewModel> : RelayCommand<TViewModel, object> where TViewModel: IHierarchicalViewModel
    {
        public CommonRemoveItemCommand(string cmdKey) : 
            base(
            context => 
            {
                var coll = context.ViewModel.Parent as IList;
                if(coll != null)
                    coll.Remove(context.ViewModel);
            },
            context => 
                context != null && 
                context.ViewModel != null &&
                context.ViewModel.Parent!=null && context.ViewModel.Parent is ICollectionViewModel &&
                !context.ViewModel.Parent.IsReadOnly, 
            cmdKey
            )
        {
        }
    }
}
